//
//
//package com.jingdun.sport.api.security;
//
//
//import cn.binarywang.wx.miniapp.api.WxMaService;
//import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
//import com.youbi.shop.security.enums.App;
//import com.youbi.shop.security.model.AppConnect;
//import com.youbi.shop.security.service.YamiUser;
//import com.youbi.shop.security.service.YamiUserDetailsService;
//import com.youbi.shop.security.exception.UsernameNotFoundExceptionBase;
//import com.youbi.shop.security.exception.WxErrorExceptionBase;
//import com.youbi.shop.security.provider.AbstractUserDetailsAuthenticationProvider;
//import lombok.AllArgsConstructor;
//import me.chanjar.weixin.common.error.WxErrorException;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.stereotype.Component;
//
///**
// * 小程序登陆
// */
//@Component
//@AllArgsConstructor
//public class MiniAppAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
//
//    private final YamiUserDetailsService yamiUserDetailsService;
//
//    private final WxMaService wxMaService;
//
//    @Override
//    protected Authentication createSuccessAuthentication(Authentication authentication, UserDetails user) {
//        MiniAppAuthenticationToken result = new MiniAppAuthenticationToken(user, authentication.getCredentials());
//        result.setDetails(authentication.getDetails());
//        return result;
//    }
//
//    @Override
//    protected UserDetails retrieveUser(String username, Authentication authentication) throws AuthenticationException {
//        YamiUser loadedUser = null;
////        // 如果使用debugger 模式，则返回debugger的用户
////        if (BooleanUtil.isTrue(((MyAuthenticationToken)authentication).getDebugger())) {
////            loadedUser = new YamiUser("1" , "debuggerOpenId1" ,  this.getAppInfo().value(), true);
////            loadedUser.setDebugger(true);
////            return loadedUser;
////        }
//        System.out.println(((AppAuthenticationToken) authentication).getGrant_type());
//
//        WxMaJscode2SessionResult session = null;
//
//        AppConnect appConnect = new AppConnect();
//        appConnect.setAppId(this.getAppInfo().value());
//        try {
//
//            session = wxMaService.getUserService().getSessionInfo(username);
//
//            loadedUser = yamiUserDetailsService.loadUserByAppIdAndBizUserId(this.getAppInfo(), session.getOpenid());
//        } catch (WxErrorException e) {
//            throw new WxErrorExceptionBase(e.getMessage());
//        } catch (UsernameNotFoundExceptionBase var6) {
//            if (session == null) {
//                throw new WxErrorExceptionBase("无法获取用户登陆信息");
//            }
//            appConnect.setBizUserId(session.getOpenid());
//            appConnect.setBizUnionid(session.getUnionid());
//            yamiUserDetailsService.insertUserIfNecessary(appConnect);
//        }
//
//        if (loadedUser == null) {
//            loadedUser = yamiUserDetailsService.loadUserByAppIdAndBizUserId(this.getAppInfo(), appConnect.getBizUserId());
//        }
//        return loadedUser;
//    }
//
//    @Override
//    public boolean supports(Class<?> authentication) {
//        return MiniAppAuthenticationToken.class.isAssignableFrom(authentication);
//    }
//
//
//    @Override
//    protected App getAppInfo() {
//        return App.MINI;
//    }
//}
